import wepy from 'wepy'
export default class extends wepy.mixin {
  data = {
    addRessInfo: null,
    cart: [],
    isLogin: false
  }
  methods = {
    async addCity() {
      const res = await wepy.chooseAddress().catch((err) => err)
      console.log(res)
      if (res.errMsg === 'chooseAddress:fail cancel') {
        return
      }
      this.addRessInfo = res
      wepy.setStorageSync('address', res)
      this.$apply()
    },
    async getuserinfo(userinfo) {
      console.log(userinfo)
      if (userinfo.detail.errMsg !== 'getUserInfo:ok') {
        return wepy.baseToast('获取用户信息失败')
      }
      const loginRes = await wepy.login()
      if (loginRes.errMsg !== 'login:ok') {
        return wepy.baseToast('微信登录失败')
      }
      const loginParams = {
        code: loginRes.code,
        encryptedData: userinfo.detail.encryptedData,
        iv: userinfo.detail.iv,
        rawData: userinfo.detail.rawData,
        signature: userinfo.detail.signature
      }
      const { data: res } = await wepy.postData('/users/wxlogin', loginParams)
      console.log(res)

      if (res.meta.status !== 200) {
        return wepy.baseToast('微信登录失败')
      }
      wepy.setStorageSync('token', res.message.token)
      this.isLogin = true
      this.$apply()
      // const {data:res} = await wepy.postData('/my/orders/create',{
      //     order_price:'',
      //     consignee_addr:'',
      //     order_detail:'',
      //     goods:''
      // })
      // console.log(res);
    },
    onSubmit() {}
  }
  onLoad() {
    this.addRessInfo = wepy.getStorageSync('address') || ''
    this.cart = this.$parent.globalData.cart.filter((x) => x.isCheck)
  }
  computed = {
    isHaveAddRess() {
      if (this.addRessInfo !== null) {
        return true
      } else {
        return false
      }
    },
    addRessCity() {
      if (this.addRessInfo === null) {
        return ''
      }
      return this.addRessInfo.provinceName + this.addRessInfo.cityName + this.addRessInfo.countyName + this.addRessInfo.detailInfo
    },
    totalPrice() {
      let total = 0
      this.cart.forEach((element) => {
        if (element.isCheck) {
          total += element.price
        }
      })
      return total * 100
    }
  }
}
